package 剑指Offer;

import java.util.HashMap;
import java.util.Map;

public class Offer48_最长不含重复字符的子字符串 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> window = new HashMap<>();
        int left = 0;
        int right = 0;
        int res = 0;//记录结果

        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
            //进行窗口内数据的更新
            window.put(c, window.getOrDefault(c, 0) + 1);

            //说明有重复的字串出现，因为名称为c的字符出现的次数大于1了，不符合条件
            while (window.getOrDefault(c, 0) > 1) {
                char d = s.charAt(left);
                left++;
                //进行窗口内数据的更新
                window.put(d, window.getOrDefault(d, 0) - 1);
            }

            //在这里更新答案
            res = Math.max(res, right - left);

        }
        return res;
    }
}
